<template>
  <div class="youke">
    <Main pt>
      <div class="content">
        <div class="logo">
          <img src="../../assets/icon/logo.png" alt>
        </div>
        <div class="form-box">
          <van-field
            v-model="requestParam.mobile"
            placeholder="请输入手机号"
            left-icon=" iconfont icondailishang_shouji-1"
            type="number"
            maxlength="11"
            clearable
            class="login-input"
          />
          <van-field
            v-model="requestParam.password"
            placeholder="请输入密码"
            left-icon=" iconfont icondailishang_shouji-3"
            clearable
            type="password"
            class="login-input"
          />
          <div class="login-footer">
            <div @click="goregister(0)">去注册</div>
            <div @click="goregister(1)">忘记密码？</div>
          </div>
          <p class="footer">
            <van-button type="info" :disabled="disabled" @click="doLogin">登录</van-button>
          </p>
        </div>
      </div>
    </Main>
    <van-popup v-model="show" :overlay="false">
      <van-loading size="50px" type="spinner" text-size="24px" vertical color="#1989fa"></van-loading>
      <!-- <div class="texts">上传中...</div> -->
    </van-popup>
  </div>
</template>

<script type="text/ecmascript-6">
import { check } from "@/ext/check";
import { user } from "@/api";

export default {
  //接收父级传递的参数
  props: [],
  //监听数据变化
  watch: {},
  data() {
    return {
      requestParam: {
        mobile: "",
        password: ""
      },
      show: false,
      disabled:false
    };
  },
  //实例初始化之前触发事件
  beforeCreate() {},
  //实例化之后触发事件
  created() {
    //  this.show=true
  },
  //实时监控data参数数据变化
  computed: {},
  //模板组件
  components: {},
  //执行方法
  methods: {
    doLogin() {
      if (!check.mobile(this.requestParam.mobile)) {
        this.$toast.fail("手机号码输入不正确");
        return false;
      }
      if (!this.requestParam.password) {
        this.$toast.fail("密码不能为空");
        return false;
      }
      //数据验证；
      let data = {
        username: this.requestParam.mobile,
        password: this.requestParam.password
      };

      this.show = true;
      this.disabled=true
      user.login(data).then(res => {
        if (res.code == 1000) {
          this.show = false;
          this.disabled=false
          this.$store.dispatch("addinfo").then(res => {
            if (res.code == 1000) {
              this.$router.push("/home");
            } else {
              this.$toast.fail(res.reason);
            }
          });
        } else {
          setTimeout(() => {
            this.show = false;
             this.disabled=false
          }, 500);
          this.$toast.fail(res.reason);
        }
      });
      // console.log(check.length(this.requestParam.mobile, 6, 6));
    },
    goregister(f) {
      if (f == 0) {
        this.$router.push("/user/register");
      } else {
        this.$router.push("/user/forget?pass=1");
      }
    }
  },
  mounted() {
    window.localStorage.clear();
    // this.$cookies.remove();
  }
  //路由退出时候调用
  //beforeRouteLeave(to, from, next){next()}
};
</script>
<style scoped lang="less">
.youke {
  height: 100%;
  background-color: #ffffff;
  overflow: hidden;
}

.content {
  height: 80.5vh;
  .logo {
    width: 7rem;
    height: 7rem;
    margin: 5rem auto 3rem;

    img {
      width: 100%;
    }
  }

  .form-box {
    width: 92%;
    margin: 0 auto;
    font-size: 14px;
    background-color: #ffffff;

    .footer {
      margin-top: 1.5rem;
      color: #808080;

      .van-cell {
        width: 50%;
        float: left;
      }

      .van-cell:last-child {
        text-align: right;
      }
    }

    button {
      width: 90%;
      display: block;
      margin: auto;
    }
  }
}

.login-footer {
  display: flex;
  justify-content: space-between;
  padding: 1rem 1rem 0.5rem;
}
.van-popup {
  background: none;
  .texts {
    color: #fff;
    margin-top: 1rem;
  }
}
</style>
<style lang="less">
.login-input {
  &.van-cell {
  }

  .van-field__control {
    text-indent: 1rem;
    font-size: 0.9rem;
    line-height: 16px;
  }

  i {
    &.icondailishang_shouji-1 {
      font-size: 1rem !important;
      color: #999;
    }

    &.icondailishang_shouji-3 {
      font-size: 1rem !important;
      color: #999;
    }
  }
}
</style>
